#!/usr/bin/env python
# encoding: utf-8
"""
test_security.py

Created by Niall Richard Murphy on 2011-06-08.
"""

import config_parse
import constants
import os
import test_infrastructure as ti
# Perhaps unittest2 is available. Try to import it, for
# those cases where we are running python 2.7.
try:
    import unittest2 as unittest
except ImportError:
    import unittest
import core.core_definitions as cd


def suite():
  tests = ['testPasswordEncryption',
           'testPasswordForConsole',
           'testSecureTransportForVTYs',
           'testSSHVersion2Defined',
	   'testSourceRouting']
  return unittest.TestSuite(map(TestBulkSecurity, tests))

class TestBulkSecurity(ti.defaultTestConfiguration):
  
  def testPasswordEncryption(self):
    results = ti.IndividualAttributesDefined(self.cp.routers, 'password_encryption')
    self.assertEqual(results, [],
      "These routers should have password encryption turned on: %s" % (results))

  def testSourceRouting(self):
    results = ti.IndividualAttributesDefined(self.cp.routers, 'source_routing')
    self.assertEqual(results, [],
      "These routers should have source routing turned off: %s" % (results))

  def testSSHVersion2Defined(self):
    results = ti.MeetsCriteriaReturnData(self.cp.routers, cd.UsingSafeSSHVersion)
    self.assertEqual(results, [],
      "These routers should have ssh enabled AND ssh version 2 defined: %s" % (results))

  def testPasswordForConsole(self):
    results = ti.MeetsCriteria(self.cp.routers, cd.HasConsolePassword)
    self.assertEqual(results, [],
      "These routers should have password protection on their console port: %s" % (results))

  def testSecureTransportForVTYs(self):
    results = ti.MeetsCriteriaReturnData(self.cp.routers, cd.HasSecureTransportForVTYs)
    self.assertEqual(results, [],
      "These routers should have SSH transport for their VTYs: %s" % (results))

  def testUnreferencedACLs(self):
    results = ti.MeetsCriteriaReturnData(self.cp.routers, cd.HasUnreferencedACL)
    self.assertEqual(results, [],
      "These routers have ACLs unreferenced by VTYs or interfaces: %s" % (results))

  def testUnappliedVTYACLs(self):
    results = ti.MeetsCriteriaReturnData(self.cp.routers, cd.HasUnACLedVTYs)
    self.assertEqual(results, [],
      "These routers have some VTYs without securing ACLs applied: %s" % (results))

if __name__ == '__main__':
  suite = suite()
  unittest.main(testRunner=unittest.TextTestRunner(verbosity=2)).run(suite)
